trigger PA_Insert_Percentage_Trigger on Productivity_Allocations__c (before insert, before update) {

if(Trigger.isInsert)
{
String errmsg = '';
Boolean isError = false;

Productivity_Allocations__c NewPA = Trigger.new[0];


Decimal Percent3 = NewPA.Allocation_Percentage__c ;
    
            
Opportunity RelatedOp = [Select Id, Opportunity_Percentage_Allocated__c, OwnerId
                    from Opportunity
                    where Id =: NewPA.Opportunity_Name__c Limit 1]; 
                    Percent3 = Percent3 + RelatedOp.Opportunity_Percentage_Allocated__c;
    if (Percent3 > 100.00){
    errmsg += 'Total Productivity Allocation percent is currently '+Percent3+'% The Total Allocation percentage cannot be greater than 100%';
    isError = true;
    NewPA.addError(errmsg);
                            }

}

    Map<ID, Set<ID>> salesAssociateForOppMap = new Map<ID, Set<ID>>();
    Set<ID> salesAssociateIDSet = new Set<ID>();
    Set<ID> opportunityIDSet = new Set<ID>();
    Map<ID, String> salesAssociateNameMap  = new Map<ID, String>();
    for(Productivity_Allocations__c pa:Trigger.new)
    {
        opportunityIDSet.add(pa.Opportunity_Name__c);
    }
    if(opportunityIDSet.size()> 0)
    {
        Map<ID, Opportunity> opportunityMap = new Map<ID, Opportunity>([select id,(select id, sales_associate__c, sales_associate__r.name from Productivity_Allocations__r) from Opportunity where id in :OpportunityIDSet]);
        for(Opportunity o: opportunityMap.values())
        {
            salesAssociateIDSet = new Set<ID>();
            for(Productivity_Allocations__c pa: o.Productivity_Allocations__r)
            {
                salesAssociateIDSet.add(pa.Sales_Associate__c);
                salesAssociateNameMap.put(pa.sales_associate__c, pa.sales_associate__r.name);
                if(Trigger.oldMap != null && Trigger.oldMap.get(pa.id) != null && Trigger.oldMap.get(pa.id).sales_associate__c == Trigger.newMap.get(pa.id).sales_associate__c)
                {
                    salesAssociateIDSet.remove(pa.sales_associate__c);
                    salesAssociateNameMap.remove(pa.sales_associate__c);
                }
            }
            if(salesAssociateIDSet.size() > 0)
                salesAssociateForOppMap.put(o.id, salesAssociateIDSet);
         }
  
      if(salesAssociateIDSet.size() > 0)
        {
            for(Productivity_Allocations__c pa:Trigger.new)
            {
                if(salesAssociateForOppMap.get(pa.opportunity_name__c).contains(pa.sales_associate__c) == true)
                {
                    pa.addError(salesAssociateNameMap.get(pa.Sales_Associate__c)+' is already assinged allocations for this particular opportunity. Please select another Sales person for this Productivity Allocations.');
                }
             }
         }
     }
 
 }